myApp.controller('additemController',function($scope,$state,toaster,$serProduct,$filter,$serAttr,$serCatalog,FileUploader,$serImgUpload) {
    var ctrl = this;
    $scope.defaultImg = {
        index : -1
    };
    ctrl.vm = {
        flag100:0,
        flag300:[],
        data:{},
        data2:{},
        search:{
            id:'',
            name:'',
            status:'',
        },
        searchFn:function(){
            vm.init();
        },
        reset:function(){
            ctrl.vm.search.id='';
            ctrl.vm.search.name='';
            ctrl.vm.search.status='';
            vm.init();
        },
        catalogData:{},
        attrData:{},
        attrTest:'',
        attrTest2:'',
        attrTestArr:[],
        biaoShi:1,
        product_stock:{
            price:'',
            bar_code:'',
            code:'',
            stock:''
        },
        testt:{},
        priceList:[],
        imgList:[],
        cat_ids:[],
        product_attr:[],
        attrSort:1,
        testB:[],
        testC:[],
        price_stock:{},
        change:function (index) {
            ctrl.vm.flag300.splice(index,1,1);
            console.log(ctrl.vm.flag300,121);
        },
        resetInit:function () {
            ctrl.vm.data= {},
            ctrl.vm.catalogData= {},
            ctrl.vm.attrData= {},
            ctrl.vm.attrTest= '',
            ctrl.vm.attrTest2= '',
            ctrl.vm.attrTestArr= [],
            ctrl.vm.biaoShi= 1,
            ctrl.vm.product_stock= {
                price :'',
                bar_code: '',
                code: '',
                stock: ''
            },
            ctrl.vm.testt= {},
            ctrl.vm.priceList= [],
            ctrl.vm.imgList= [],
            ctrl.vm.cat_ids= [],
            ctrl.vm.product_attr= [],
            ctrl.vm.attrSort= 1,
            ctrl.vm.testB= [],
            ctrl.vm.testC= [],
            ctrl.vm.price_stock= {}
        },

        //提交
        ok:function(){
            var start = $filter('date')(ctrl.vm.data.startTime,'yyyy-MM-dd 00:00:01');
            var end = $filter('date')(ctrl.vm.data.endTime,'yyyy-MM-dd 23:59:59');
            if($scope.defaultImg.index ===-1 ){ //默认图单选按钮
            ctrl.vm.cat_ids= [],
                angular.forEach(ctrl.vm.catalogData,function(v,i){
                    if(!!v.select){
                        ctrl.vm.cat_ids.push(v.id)
                    }
                    angular.forEach(v.children,function(m,n){
                        if(!!m.select){
                            ctrl.vm.cat_ids.push(m.id)
                        }
                        angular.forEach(m.children,function(x,y){
                            if(!!x.select){
                                ctrl.vm.cat_ids.push(x.id)
                            }
                        })
                    })
                });
                $serProduct.addIt({
                    type:1,

                    name:ctrl.vm.data.name,
                    promo_words:ctrl.vm.data.promo_words,
                    price:ctrl.vm.data.price,
                    short_description:ctrl.vm.data.short_description,
                    description:ctrl.vm.data.description,
                    weight:ctrl.vm.data.weight,
                    new_start:start,
                    new_end:end,
                    status:ctrl.vm.data.status,
                    is_search:ctrl.vm.data.is_search,

                    cat_ids:ctrl.vm.cat_ids.join(","),

                    product_attr:ctrl.vm.product_attr,
                    product_stock:ctrl.vm.testC,

                    images:ctrl.vm.imgList


                }).then(function(res){
                    if(res.code==1){
                        vm.init();
                        toaster.success('提示',res.message);
                    }else{
                        toaster.info('提示',res.message);
                    }
                })
                return;
            }
            angular.forEach(ctrl.vm.imgList, function(data) { //重置单选按钮
                data.is_default=0;
            })
            ctrl.vm.imgList[$scope.defaultImg.index].is_default=1;

            ctrl.vm.cat_ids= [],
            angular.forEach(ctrl.vm.catalogData,function(v,i){
                if(!!v.select){
                    ctrl.vm.cat_ids.push(v.id)
                }
                angular.forEach(v.children,function(m,n){
                    if(!!m.select){
                        ctrl.vm.cat_ids.push(m.id)
                    }
                    angular.forEach(m.children,function(x,y){
                        if(!!x.select){
                            ctrl.vm.cat_ids.push(x.id)
                        }
                    })
                })
            });
            console.log(ctrl.vm.cat_ids);
            $serProduct.addIt({
                type:1,

                name:ctrl.vm.data.name,
                promo_words:ctrl.vm.data.promo_words,
                price:ctrl.vm.data.price,
                short_description:ctrl.vm.data.short_description,
                description:ctrl.vm.data.description,
                weight:ctrl.vm.data.weight,
                new_start:start,
                new_end:end,
                status:ctrl.vm.data.status,
                is_search:ctrl.vm.data.is_search,

                cat_ids:ctrl.vm.cat_ids.join(","),

                product_attr:ctrl.vm.product_attr,
                product_stock:ctrl.vm.testC,

                images:ctrl.vm.imgList


            }).then(function(res){
                if(res.code==1){
                    ctrl.vm.resetInit();
                    vm.init();
                    toaster.success('提示',res.message);
                }else{
                    toaster.info('提示',res.message);
                }
            })
        },
        //属性向右
        toright:function(next,twoList){
            angular.forEach(twoList,function(i,j){
                angular.forEach(next.split(','),function(k,l){
                    if( i.id == k ){
                        ctrl.vm.attrTestArr.push(i);
                    }
                })
            });
            var a=[];b=[];c=[];
            for(var i = 0; i < ctrl.vm.attrTestArr.length ; i++){
                if( !a[ ctrl.vm.attrTestArr[i].id ]){
                    a[ ctrl.vm.attrTestArr[i].id ] =true;
                    b.push(ctrl.vm.attrTestArr[i]);
                }else{
                    c.push(ctrl.vm.attrTestArr[i].name);
                }
            }
            ctrl.vm.attrTestArr = b;
            ctrl.vm.product_attr=[];
            for(var i=0;i<ctrl.vm.attrTestArr.length;i++){
                ctrl.vm.product_attr.push({attr_id:ctrl.vm.attrTestArr[i].id,sort:ctrl.vm.attrSort});
            }

            if(c.length>0){
                toaster.info('提示',c+'已添加')
            }
        },
        //属性向左
        toleft:function(lastModel,lastList){
            angular.forEach(lastModel,function(i,j){
                angular.forEach(lastList,function(k,l){
                    if( i == k.id ){
                        ctrl.vm.attrTestArr.splice(l,1);
                    }
                })
            })
        },
        //属性的属性显示
        changeAttrMall:function(index,list){
            //console.log(1,index);
        },
        //属性增加组合
        addZuhe:function(){
            angular.forEach(ctrl.vm.priceList, function(data) { //解决组合重复添加
                var a = data.one.name||undefined;
                var b = data.two.name||undefined;
                var c = data.san.name||undefined;
                var d = a + b + c;
                var e = JSON.parse(ctrl.vm.testt.one||'[]').name + JSON.parse(ctrl.vm.testt.two||'[]').name + JSON.parse(ctrl.vm.testt.san||'[]').name
                if (d === e) {
                    ctrl.vm.priceList.pop();
                    toaster.info('此组合已存在');
                }
            })
            ctrl.vm.priceList.push({'one':JSON.parse(ctrl.vm.testt.one||'[]'),'two':JSON.parse(ctrl.vm.testt.two||'[]'),'san':JSON.parse(ctrl.vm.testt.san||'[]'),price:'',bar_code:'',code:'',stock:'',image:''});
            console.log(ctrl.vm.priceList,'hh');
        },
        //删除单个属性
        priceDel:function($index){
            ctrl.vm.priceList.splice($index,1);
            ctrl.vm.testC.splice($index,1);
            },
        //确认单个属性
        priceSev:function(index,list){
            if(list.one.id){
                ctrl.vm.testB = [list.one.id];
            }
            if(list.two.id){
                ctrl.vm.testB = [list.one.id,list.two.id];
            }
            if(list.san.id){
                ctrl.vm.testB = [list.one.id,list.two.id,list.san.id];
            }

            angular.forEach(ctrl.vm.testC, function(data){ //解决组合重复确认
                var a = data.attr_value_set.toString();
                var b = ctrl.vm.testB.toString()
                if(a===b){
                    ctrl.vm.testC.pop();
                }
            });
            ctrl.vm.testC.push({attr_value_set:ctrl.vm.testB,price:list.price,bar_code:list.bar_code,code:list.code,stock:list.stock,image:list.image});
            ctrl.vm.flag300[index] = undefined;
        },
        //属性重置
        resetProperty:function(){
            ctrl.vm.priceList=[];
            ctrl.vm.testC=[];
            ctrl.vm.product_stock={}
            ctrl.vm.flag100=0
        },
        //属性返回第一步
        gotoOne:function(){
            ctrl.vm.biaoShi=1;
            ctrl.vm.priceList=[];
        },
        //图片增加
        addImg:function(){
            ctrl.vm.imgList.push({url:'',alt:'',sort:'',is_default:'0'});
        },
        //删除单个图片行
        imgDel:function(index){
            ctrl.vm.imgList.splice(index,1)
        },
        up:function () {
            ctrl.vm.testC=[];
            ctrl.vm.testC.push({attr_value_set:[],price:ctrl.vm.product_stock.price,bar_code:ctrl.vm.product_stock.bar_code,code:ctrl.vm.product_stock.code,stock:ctrl.vm.product_stock.stock,image:''});
            ctrl.vm.flag100=0
        }
    };
    $scope.vm={
        page:{}
    };
    /*跳转页面*/
    $scope.pageChanged = function() {
        vm.init($scope.vm.page.current_page)
    };
    /*每页显示数量显示*/
    $scope.placement = {
        options: [5,10,20,50],
        pageSize:5,
        selected: 20,
        changeFn:function(){
            vm.init(0)
        }
    };

    //获取分类和属性
    var vm = {
        init:function(page){
            $serCatalog.getAll({   //获取分类

            }).then(function(res){
                if(res.code==1){
                    ctrl.vm.catalogData = res.data;
                }else{
                    toaster.info('提示',res.message);
                }

            });
            $serProduct.getList({
                page:page||1,
                size:$scope.placement.selected,
                name:ctrl.vm.search.name||'',
            }).then(function(res){
                if(res.code==1){
                    ctrl.vm.data2 = res.data;
                    $scope.vm.page = res.addtion_data;
                }else{
                    ctrl.vm.data ="";
                    toaster.info('提示',res.message);
                }
            })
            $serProduct.getEnableAttrList({  //获取属性
                page:1,
                size:100
            }).then(function(res){
                if(res.code==1){
                    ctrl.vm.attrData = res.data;
                }else{
                    toaster.info('提示',res.message);
                }
            })
        }
    };
    vm.init();
    //图片上传
    var uploader = $scope.uploader = $serImgUpload.setImgUpload('p_',true);

    $scope.clearItems = function (index) {
        $serImgUpload.putImgUpload(uploader,'p',function(){
            $serImgUpload.upload(uploader);

        });
        uploader.onSuccessItem = function(fileItem) {
            ctrl.vm.imgList[index].url = fileItem.file.name;
        };
    }
    var uploaders = $scope.uploaders = $serImgUpload.setImgUpload('p_norms_',true);
    
    $scope.clearItemss = function (index) {
        $serImgUpload.putImgUpload(uploaders,'p_norms',function(){
            $serImgUpload.upload(uploaders);
    
        });
        uploaders.onSuccessItem = function(fileItem) {
            ctrl.vm.priceList[index].image = fileItem.file.name;
            ctrl.vm.flag300[index]=1;
        };
    }

    //分类选择

    $scope.changeFn = function(v,s,c){
        if(s==1){   //控制器
            angular.forEach(v.children,function(m,n){
                m.select = v.select;
            })
        }else if(s==2){  //方法
            var counter = 1;
            angular.forEach(c.children,function(m,n){
                if(m.select){
                    counter++;
                }
            });
            c.select = counter>1?true:false;
        } else{  //方法
            var counter1 = 0;
            angular.forEach(c.children,function(m,n){
                if(m.select){
                    counter1++;
                }
            });
            c.select = counter1>0?true:false;
        }
    };
    //时间选择
    $scope.open = function() {
        $scope.popup.opened = true;
    };
    $scope.popup = {
        opened: false
    };
    $scope.open1 = function() {
        $scope.popup1.opened = true;
    };
    $scope.popup1 = {
        opened: false
    };

});